package com.king.headline.UI;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.king.headline.R;
import com.king.headline.base.BaseActivity;
import com.king.headline.beans.UserInfo;
import com.king.headline.net.Callback;
import com.king.headline.net.NetWork;
import com.king.headline.util.AccountManager;
import com.king.headline.util.CustomToast;
import com.king.headline.util.DialogUtils;
import com.king.headline.util.KeyBoardUtils;
import com.king.headline.util.UIUtils;

import org.json.JSONObject;

import butterknife.BindView;

/**
 * 修改密码界面
 *
 * @author Administrator
 */
public class ModifyPswActivity extends BaseActivity implements OnClickListener {
    @BindView(R.id.bt_loging)
    Button bt_loging;
    @BindView(R.id.et_oldPsw)
    EditText et_oldPsw;
    @BindView(R.id.et_password)
    EditText et_password;
    @BindView(R.id.et_password_ensure)
    EditText et_password_ensure;
    @BindView(R.id.item_main)
    View item_main;
    String password;
    String oldPsw;
    ProgressDialog progressDialog;
    ProgressDialog pDialog;
    String password_ensure;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UIUtils.addActivity(this);
        baseSetContentView(R.layout.activity_modifypwd);
        init();
        setTopBarAndAction();
    }


    private void init() {
        setBackIcon(true);
        bt_loging.setOnClickListener(this);
        //点击空白处关闭键盘
        item_main.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                KeyBoardUtils.closeKeybord(et_oldPsw);
                return false;
            }
        });

    }

    private void setTopBarAndAction() {
        setBarTitleText("修改密码"); // 设置Title标题
    }


    private void login() {
        // 获取登录信息
        oldPsw = et_oldPsw.getText().toString().trim();
        password = et_password.getText().toString().trim();
        password_ensure = et_password_ensure.getText().toString().trim();
        if (TextUtils.isEmpty(oldPsw)) {
            DialogUtils.showHintDialogOne(this, "原密码不能为空");
        } else if (TextUtils.isEmpty(password)) {
            DialogUtils.showHintDialogOne(this, "新密码不能为空");
        } else if (!password.equals(password_ensure)) {
            DialogUtils.showHintDialogOne(this, "两次密码输入不一致");
        } else {
            //登录
            if (pDialog == null) {
                pDialog = ProgressDialog.show(this, "", UIUtils.getString(R.string.progress_content));
            }
            UserInfo userInfo = AccountManager.getInstance().getUser();
            NetWork.getInstance().modifyPsw(userInfo.encyptdId,userInfo.apikey,oldPsw, password, new Callback() {
                @Override
                public void success(String result) {
                    DialogUtils.showHintDialogOne(ModifyPswActivity.this, result + "，请重新登陆", new DialogUtils.OnDialogListener() {
                        @Override
                        public void onOk(View v) {
                            AccountManager.getInstance().logout();
                            ModifyPswActivity.this.finish();
                        }

                        @Override
                        public void onCancel(View v) {

                        }
                    });


                }

                @Override
                public void notice(JSONObject jsonObject, String message) {
                    if (pDialog != null) {
                        pDialog.dismiss();
                        pDialog = null;
                    }
                    CustomToast.showToast(message);
                }

                @Override
                public void failed(String e) {
                    if (pDialog != null) {
                        pDialog.dismiss();
                        pDialog = null;
                    }
                    CustomToast.showToast(e);
                }
            });
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_loging:
                login();
                break;

            case R.id.tv_getPwd:
                startActivity(new Intent(this, GetPswActivity.class));
                break;

            default:
                break;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UIUtils.removeActivity(this);
    }


}
